package com.sunyard.utils;

import com.alibaba.fastjson.JSONObject;
import com.sunyard.base.RequestInfo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Map;

/**
 * @Author 张昊坤
 * @Description 特定日志输出
 * @Date 2021/11/30 16:11
 */
@Slf4j
public class LogUtil {


    /**
     * 特定日志输出类
     *
     * @return
     */
    public static Logger getLogger() {
        return LoggerFactory.getLogger("zhsd java sdk");
    }


    /**
     * 响应返回
     *
     * @param transType
     * @param apiFlag
     * @param outTradeNo
     * @param mchntCd
     * @param staffId
     */
    public static void logoutResp(String transType, String apiFlag, String outTradeNo, String mchntCd, String staffId, long startTime, long endTime) {

        String start = DateUtil.getDateStr("yyyy-MM-dd HH:mm:ss.SSS", new Date(startTime));
        String end = DateUtil.getDateStr("yyyy-MM-dd HH:mm:ss.SSS", new Date(endTime));
        String sum = String.valueOf(endTime - startTime);
        //ZHSD|线程|服务名称|交易码|接口调用方|订单号|商户号|门店号|店员号|时间|
        getLogger().info("respCode:00|respMsg:{}|transType:{}|apiFlag:{}|outTradeNo:{}|mchntCd:{}|staffId:{}|startTime:{}|endTime:{}|sumTime:{}",
                StringUtil.addRight("交易成功", 15), StringUtil.addRight(transType, 13), StringUtil.addRight(apiFlag, 1),
                StringUtil.addRight(outTradeNo, 24), StringUtil.addRight(mchntCd, 15), StringUtil.addRight(staffId, 4),
                start, end, StringUtil.addRight(sum, 5));

    }

    public static void logoutResp(String transType, long startTime) {
        logoutResp(transType, "", "", "", "", startTime, System.currentTimeMillis());
    }

    /**
     * 错误返回
     *
     * @param msg
     */
    public static void logoutError(String msg, String transType, String apiFlag, String outTradeNo, String mchntCd, String staffId, long startTime, long endTime) {

        String start = DateUtil.getDateStr("yyyy-MM-dd HH:mm:ss.SSS", new Date(startTime));
        String end = DateUtil.getDateStr("yyyy-MM-dd HH:mm:ss.SSS", new Date(endTime));
        String sum = String.valueOf(endTime - startTime);
        getLogger().info("respCode:01|respMsg:{}|transType:{}|apiFlag:{}|outTradeNo:{}|mchntCd:{}|staffId:{}|startTime:{}|endTime:{}|sumTime:{}",
                StringUtil.addRight(msg, 15), StringUtil.addRight(transType, 13), StringUtil.addRight(apiFlag, 1),
                StringUtil.addRight(outTradeNo, 24), StringUtil.addRight(mchntCd, 15), StringUtil.addRight(staffId, 4),
                start, end, StringUtil.addRight(sum, 5));
    }

    public static void logoutError(String msg, long startTime, String transType) {
        logoutError(msg, transType, "", "", "", "", startTime, System.currentTimeMillis());
    }

    public static void logoutError(String msg, long startTime, Object[] objects) {
        Object transType = "获取失败";
        for (Object arg : objects) {
            if (arg instanceof RequestInfo) {
                transType = getJSONObject((RequestInfo) arg).get("transType");
            }
        }
        logoutError(msg, transType.toString(), "", "", "", "", startTime, System.currentTimeMillis());
    }



    /**
     * getJsonObject
     * 判断 格式转换
     */
    private static JSONObject getJSONObject(RequestInfo requestInfo) {
        JSONObject jsonObject = null;
        if (requestInfo.getData() instanceof String) {
            jsonObject = (JSONObject) JSONObject.parse(requestInfo.getData().toString());
        } else {
            jsonObject = (JSONObject) JSONObject.toJSON(requestInfo.getData());
        }
        return jsonObject;
    }

}
